package com.yida.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.yida.system.api.domain.request.BindCommunityInfo;
import org.springframework.stereotype.Service;

import com.yida.system.api.domain.SysUserCommunity;
import com.yida.system.mapper.SysUserCommunityMapper;
import com.yida.system.service.ISysUserCommunityService;

/**
 * 复用ppv SysUserCommunityMapper
 *
 * @author hmh
 * @date 2023/03/24
 */
@Service
public class SysUserCommunityServiceImpl implements ISysUserCommunityService {

    @Resource
    SysUserCommunityMapper sysUserCommunityMapper;


    /*
     * 根据用户ID获取绑定小区ID
     */
    @Override
    public List<SysUserCommunity> getBindCommunityByUserId(Long userId) {
        List<SysUserCommunity> sysUserCommunityList = sysUserCommunityMapper.getBindCommunities(userId);
        return sysUserCommunityList;
    }

    /**
     * 根据用户ID获取绑定小区ID
     */
    @Override
    public List<Long> getCommunityIdsByUserId(Long userId) {
        List<SysUserCommunity> sysUserCommunityList = getBindCommunityByUserId(userId);
        // 判断当前List是否为空，空则输出空集合，否则根据communityId进行集合输出
        return Optional.ofNullable(sysUserCommunityList).orElse(new ArrayList<SysUserCommunity>()).stream().map(SysUserCommunity::getCommunityId).collect(Collectors.toList());
    }

    /*
     * 根据用户ID删除绑定小区
     */
    @Override
    public boolean delUserAndCommunityByUserId(Long userId) {
        return sysUserCommunityMapper.delUserAndCommunityByUserId(userId);
    }

    @Override
    public boolean insertBatchUserAndCommunity(Long userId, List<BindCommunityInfo> infos) {
        infos.stream().forEach(item -> {
            sysUserCommunityMapper.insertBatchUserAndCommunity(userId, item.getStreetId(), item.getCommunityIds());
        });
        return true;
    }


}
